package org.homly.admin.action.product;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.homly.sql.SQLTool;

public class UpdateProductSortAction extends Action {
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id= request.getParameter("id");
		// 0：置底 1：置顶
		String type = request.getParameter("type");
		String typeId = request.getParameter("productTypeId");
		if(type.equals("1")){ 
			String sql = "select max(product_sort_no) productSortNo from product";
			List<HashMap<String, String>> list = SQLTool.query(sql);
			if(list != null && list.size() > 0) {
				int sortNo = Integer.parseInt(list.get(0).get("productSortNo"));
				sortNo ++;
				sql = "update product set product_sort_no=? where product_id=?";
				SQLTool.update(sql, new Object[]{sortNo, id});
			}
		}else if(type.equals("0")) {
			String sql = "select min(product_sort_no) productSortNo from product";
			List<HashMap<String, String>> list = SQLTool.query(sql);
			if(list != null && list.size() > 0) {
				int sortNo = Integer.parseInt(list.get(0).get("productSortNo"));
				sortNo --;
				sql = "update product set product_sort_no=? where product_id=?";
				SQLTool.update(sql, new Object[]{sortNo, id});
			}
		}
		return new ActionForward(mapping.findForward("success").getPath() + "?id=" + typeId);
	}

}
